/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.fzhucc.modules.system.repository;

import com.fzhucc.modules.system.domain.Role;
import com.fzhucc.modules.system.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @author Zheng Jie
 * @date 2018-11-22
 */
public interface UserRepository extends JpaRepository<User, Long>, JpaSpecificationExecutor<User> {

    /**
     * 根据用户名查询
     *
     * @param username 用户名
     * @return /
     */
    User findByUsername(String username);

    /**
     * 根据username查询list
     *
     * @param username /
     * @return /
     */
    @Query(value = "SELECT r.* FROM sys_user as r where r.username=?1", nativeQuery = true)
    List<User> findListsByUsername(String username);

    /**
     * 根据邮箱查询
     *
     * @param email 邮箱
     * @return /
     */
    //User findByEmail(String email);

    /**
     * 根据手机号查询
     *
     * @param phone 手机号
     * @return /
     */
    User findByPhone(String phone);

    /**
     * 根据手机号查询
     *
     * @param phone /
     * @return /
     */
    @Query(value = "SELECT u.* FROM sys_user u WHERE" +
            " u.phone = ?1", nativeQuery = true)
    List<User> findAllsByPhone(String phone);

    /**
     * 根据openId
     *
     * @param unionId
     * @return /
     */
    User findByUnionId(String unionId);


    /**
     * 根据openId
     *
     * @param unionId
     * @return /
     */
    boolean existsByUnionId(String unionId);

    /**
     * 修改密码
     *
     * @param username              用户名
     * @param pass                  密码
     * @param lastPasswordResetTime /
     */
    @Modifying
    @Query(value = "update sys_user set password = ?2 , pwd_reset_time = ?3 where username = ?1", nativeQuery = true)
    void updatePass(String username, String pass, Date lastPasswordResetTime);

    /**
     * 修改密码
     *
     * @param username              用户名
     * @param userId                用户id
     */
    @Modifying
    @Query(value = "update sys_user set username = ?1 where user_id = ?2", nativeQuery = true)
    void updateMyUserName(String username, Long userId);

    /**
     * 修改昵称
     *
     * @param nickName 昵称
     * @param userId 用户id
     */
    @Modifying
    @Query(value = "update sys_user set nick_name = ?1 where user_id = ?2", nativeQuery = true)
    void updateNicknameByUserId(String nickName, Long userId);

    /**
     * 修改头像
     *
     * @param avatarPath 头像
     * @param userId 用户id
     */
    @Modifying
    @Query(value = "update sys_user set avatar_path = ?1 where user_id = ?2", nativeQuery = true)
    void updatePicByUserId(String avatarPath, Long userId);

    /**
     * 修改邮箱
     *
     * @param username 用户名
     * @param email    邮箱
     */
    /*@Modifying
    @Query(value = "update sys_user set email = ?2 where username = ?1", nativeQuery = true)
    void updateEmail(String username, String email);*/

    /**
     * 修改昵称
     *
     * @param username 用户名
     * @param nickname 昵称
     */
    @Modifying
    @Query(value = "update sys_user set nick_name = ?2 where username = ?1", nativeQuery = true)
    void updateNickname(String username, String nickname);

    /**
     * 修改手机号
     *
     * @param user_id 用户id
     * @param phone   邮箱
     */
    @Modifying
    @Query(value = "update sys_user set phone = ?1 where user_id = ?2", nativeQuery = true)
    void updatePhone(String phone, Long user_id);

    /**
     * 根据角色查询用户
     *
     * @param roleId /
     * @return /
     */
    @Query(value = "SELECT u.* FROM sys_user u, sys_users_roles r WHERE" +
            " u.user_id = r.user_id AND r.role_id = ?1", nativeQuery = true)
    List<User> findByRoleId(Long roleId);

    /**
     * 根据角色中的部门查询
     *
     * @param deptId /
     * @return /
     */
    @Query(value = "SELECT u.* FROM sys_user u, sys_users_roles r, sys_roles_depts d WHERE " +
            "u.user_id = r.user_id AND r.role_id = d.role_id AND d.dept_id = ?1 group by u.user_id", nativeQuery = true)
    List<User> findByRoleDeptId(Long deptId);

    /**
     * 根据菜单查询
     *
     * @param id 菜单ID
     * @return /
     */
    @Query(value = "SELECT u.* FROM sys_user u, sys_users_roles ur, sys_roles_menus rm WHERE\n" +
            "u.user_id = ur.user_id AND ur.role_id = rm.role_id AND rm.menu_id = ?1 group by u.user_id", nativeQuery = true)
    List<User> findByMenuId(Long id);

    /**
     * 根据Id删除
     *
     * @param ids /
     */
    void deleteAllByIdIn(Set<Long> ids);

    /**
     * 根据岗位查询
     *
     * @param ids /
     * @return /
     */
    @Query(value = "SELECT count(1) FROM sys_user u, sys_users_jobs j WHERE u.user_id = j.user_id AND j.job_id IN ?1", nativeQuery = true)
    int countByJobs(Set<Long> ids);

    /**
     * 根据部门查询
     *
     * @param deptIds /
     * @return /
     */
    @Query(value = "SELECT count(1) FROM sys_user u WHERE u.dept_id IN ?1", nativeQuery = true)
    int countByDepts(Set<Long> deptIds);

    /**
     * 根据角色查询
     *
     * @param ids /
     * @return /
     */
    @Query(value = "SELECT count(1) FROM sys_user u, sys_users_roles r WHERE " +
            "u.user_id = r.user_id AND r.role_id in ?1", nativeQuery = true)
    int countByRoles(Set<Long> ids);

    @Query(nativeQuery = true, value = "select count(*) from sys_user where  org_area like:area or h5area like:area ")
    int findCountNormalByLikeArea(String area);


}
